﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;
using SmartSpace.UI.Silverlight.UI;

namespace SmartSpace.UI.Silverlight.Dynamic.Windows
{
    public partial class SelectReceiverWindow : SlWindow
    {
        public Guid WorkflowId { get; private set; }
        public Guid ActivityId { get; private set; }
        public WorkflowEvent WorkflowEvent { get; private set; }
        public List<Guid> ReceiverIdList { get; private set; }

        public SelectReceiverWindow(Guid workflowId, Guid activityId, WorkflowEvent workflowEvent)
        {
            InitializeComponent();

            WorkflowId = workflowId;
            ActivityId = activityId;
            WorkflowEvent = workflowEvent;
            ReceiverIdList = new List<Guid>();

            this.Loaded += new RoutedEventHandler(SelectReceiverWindow_Loaded);
        }

        void SelectReceiverWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DynamicWebServiceSoapClient client = DynamicModule.CreateProxy();
            client.GetEventReceiversCompleted += new EventHandler<GetEventReceiversCompletedEventArgs>(client_GetEventReceiversCompleted);
            client.GetEventReceiversAsync(this.WorkflowId, this.ActivityId, this.WorkflowEvent.Id);
        }

        void client_GetEventReceiversCompleted(object sender, GetEventReceiversCompletedEventArgs e)
        {
            if (e.Error != null)
            {
            }
            else if (!e.Result.IsSuccess)
            {
            }
            else
            {
                this._ctlListUser.ItemsSource = e.Result.Data;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlListUser.SelectedItem == null)
            {
                MessageBox.Show("必须选择一个用户!");
                return;
            }
            WorkflowUser user = _ctlListUser.SelectedItem as WorkflowUser;
            ReceiverIdList.Add(user.Id);

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

